package com.bluedot.www.core.service;

import com.bluedot.www.core.common.HandwritingRuntimeException;
import com.bluedot.www.core.common.ResultCodeEnum;
import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.*;
import com.bluedot.www.core.pojo.DTO.Pager;
import com.bluedot.www.core.pojo.VO.*;
import com.bluedot.www.core.pojo.query.*;
import com.bluedot.www.core.utils.CommonUtil;
import com.bluedot.www.core.utils.IpUtil;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>所有带有条件的查询</p>
 * 包括不同角色的分页查询，组合条件查询
 *
 * @author He Peng
 * @version 1.0
 * @date 2021/8/8 8:13
 */
public class QueryService {

    private final String baseMapper = "com.bluedot.www.core.mapper.xml.QueryByPageMapper.";
    private final String sqlCondition = "1 OR 1";

    public static Object execute(String methodName, Object[] param) {
        QueryService queryService = new QueryService();
        Object result = null;
        switch (methodName) {
            case "queryCustomerInfoByPage":
                result = queryService.queryCustomerInfoByPage((Long) param[0], (Integer) param[1],
                        (Integer) param[2], (CustomerQuery) param[3]);
                break;
            case "queryUserInfoByPage":
                result = queryService.queryUserInfoByPage((Integer) param[0], (Integer) param[1], (UserQuery) param[2]);
                break;
            case "queryAlgorithmInfoByPage":
                result = queryService.queryAlgorithmInfoByPage((Integer) param[0],
                        (Integer) param[1], (AlgorithmQuery) param[2]);
                break;
            case "queryFeatureByPage":
                result = queryService.queryFeatureByPage((Integer) param[0], (Integer) param[1], (FeatureQuery) param[2]);
                break;
            case "queryAuditInfoByPage":
                result = queryService.queryAuditInfoByPage((Integer) param[0], (Integer) param[1], (AuditQuery) param[2]);
                break;
            case "queryBackupByPage":
                result = queryService.queryBackupByPage((Integer) param[0], (Integer) param[1], (BackupQuery) param[2]);
                break;
            case "queryCustomerHistoryInfo":
                result = queryService.queryCustomerHistoryInfo((Integer) param[0],
                        (Integer) param[1], (ReportQuery) param[2]);
                break;
            case "queryUserHistoryInfo":
                result = queryService.queryUserHistoryInfo((Integer) param[0], (Integer) param[1], (ReportQuery) param[2]);
                break;
            case "queryRightInfo":
                result = queryService.queryRightInfo((Integer) param[0], (Integer) param[1], (RightQuery) param[2]);
                break;
            case "queryRoleInfo":
                result = queryService.queryRoleInfo((Integer) param[0], (Integer) param[1]);
                break;
            case "queryLogInfo":
                result = queryService.queryLogInfo((Integer) param[0], (Integer) param[1], (LogQuery) param[2]);
                break;
            case "queryCustomerNoAnalysis":
                result = queryService.queryCustomerNoAnalysis((Integer) param[0], (Integer) param[1],
                        (CustomerHistoryQuery) param[2]);
                break;
            default:
                throw new HandwritingRuntimeException(ResultCodeEnum.METHOD_NOT_FOUND);
        }
        return result;
    }

    // ######测试代码#######
    /*public static void main(String[] args) {
     *//*        UserQuery userQuery = new UserQuery();
//        userQuery.setName("张山");
//        userQuery.setRoleName("普通用户");
        Object[] params = new Object[] {2, 2, userQuery};
        Object queryUserInfoByPage = QueryService.execute("queryUserInfoByPage", params);
        System.out.println(queryUserInfoByPage);
        System.out.println(((Pager) queryUserInfoByPage).getRecords());*//*


     *//*CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setSort(5);
        Object[] params = new Object[]{new Long(191213687), 1, 5, customerQuery};
        QueryService.execute( "queryCustomerInfoByPage", params);*//*


     *//*AlgorithmQuery algorithmQuery = new AlgorithmQuery();
        algorithmQuery.setType(0);
        Object[] params = new Object[]{1, 5, algorithmQuery};
        Pager<ViewAlgorithmInfo> page = (Pager<ViewAlgorithmInfo>) QueryService.execute("queryAlgorithmInfoByPage", params);
        System.out.println(page);*//*

     *//*FeatureQuery featureQuery = new FeatureQuery();
        featureQuery.setStatus(1);
        Object[] params = new Object[]{1, 5, featureQuery};
        Pager<FeatureStatus> page = (Pager<FeatureStatus>) QueryService.execute("queryFeatureByPage", params);
        System.out.println(page);*//*

     *//*AuditQuery auditQuery = new AuditQuery();
        auditQuery.setAuditResult(0);
        Object[] param = new Object[]{1, 5, auditQuery};
        Pager<ViewAuditInfo> page = (Pager<ViewAuditInfo>) QueryService.execute("queryAuditInfoByPage", param);
        System.out.println(page);*//*

     *//*BackupQuery backupQuery = new BackupQuery();
        backupQuery.setBeginDate("2021-08-31");
        backupQuery.setEndDate("2021-09-31");
        Object[] params = new Object[]{1, 5, backupQuery};
        Pager<Backup> page = (Pager<Backup>) QueryService.execute("queryBackupByPage", params);
        System.out.println(page);*//*

     *//*ReportQuery reportQuery = new ReportQuery();
        reportQuery.setId("1");
        Object[] params = new Object[] {1, 5, reportQuery};
        Pager<ViewCustomerHistoryInfo> page = (Pager<ViewCustomerHistoryInfo>) QueryService.execute("queryCustomerHistoryInfo", params);
        System.out.println(page);*//*

        ReportQuery reportQuery = new ReportQuery();
        reportQuery.setId("4");
        Object[] params = new Object[] {1, 5, reportQuery};
        Pager<ViewUserHistoryInfo> page = (Pager<ViewUserHistoryInfo>) QueryService.execute("queryUserHistoryInfo", params);
        System.out.println(page);


    }*/

    /**
     * 根据条件查询客户的总人数
     *
     * @param params: 所有的条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 8:22
     */
    private Integer countCustomerCondition(Object[] params) {
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countCustomerCondition", params);
        Integer total = 0;
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }

    /**
     * 分页查询客户信息
     *
     * @param userId        : 用户编号
     * @param currentPage   : 当前页数
     * @param pageSize      : 页面大小
     * @param customerQuery : 客户条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.DO.Customer>
     * @author He Peng
     * @date 2021/8/8 9:22
     */
    private Pager<Customer> queryCustomerInfoByPage(Long userId, Integer currentPage, Integer pageSize, CustomerQuery customerQuery) {

        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> userIdMap = new HashMap<>(1);
        Map<String, Object> sortMap = new HashMap<>(1);
        Map<String, Object> professionalMap = new HashMap<>(1);
        Map<String, Object> nameMap = new HashMap<>(1);
        Map<String, Object> groupIdMap = new HashMap<>(1);

        Pager<Customer> page = new Pager<>(currentPage, pageSize);

        if (customerQuery == null) {
            customerQuery = new CustomerQuery();
        }
        if (CommonUtil.isEmpty(customerQuery.getName())) {
            customerQuery.setName(sqlCondition);
        }
        if (CommonUtil.isEmpty(customerQuery.getProfessional())) {
            customerQuery.setProfessional(sqlCondition);
        }
        if (customerQuery.getGroupId() == null) {
            groupIdMap.put("groupId", sqlCondition);
        } else {
            groupIdMap.put("groupId", customerQuery.getGroupId());
        }
        if (customerQuery.getSort() == null) {
            sortMap.put("sort", sqlCondition);
        } else {
            sortMap.put("sort", customerQuery.getSort());
        }
        userIdMap.put("userId", userId);
        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        professionalMap.put("professional", customerQuery.getProfessional());
        nameMap.put("name", customerQuery.getName());

        Object[] params = new Object[]{fromMap, toMap, userIdMap, professionalMap, nameMap, sortMap, groupIdMap};
        Integer total = countCustomerCondition(new Object[]{userIdMap, professionalMap, nameMap, sortMap, groupIdMap});
        List<Customer> records = (List<Customer>) Execute.execute(Execute.QUERY_TYPE, baseMapper + "queryCustomerInfoByPage", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }


    private Integer countUserRoleInfoCondition(Map<String, Object> roleNameMap,
                                               Map<String, Object> nameMap,
                                               Map<String, Object> statusMap,
                                               Map<String, Object> usertypeMap) {
        Object[] params = new Object[]{roleNameMap, nameMap, statusMap, usertypeMap};
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countUserRoleInfoCondition", params);
        int total = 0;
        if (tmp != null) {
            total = tmp.get(0);
        }
        return total;
    }

    /**
     * 分页查询用户信息
     *
     * @param currentPage : 当前页数
     * @param pageSize    : 页面大小
     * @param userQuery   : 用户条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.VO.ViewUserInfo>
     * @author He Peng
     * @date 2021/8/8 9:51
     */
    private Pager<ViewUserRoleInfo> queryUserInfoByPage(Integer currentPage, Integer pageSize, UserQuery userQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> roleNameMap = new HashMap<>(1);
        Map<String, Object> nameMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);
        Map<String, Object> usertypeMap = new HashMap<>(1);

        Pager<ViewUserRoleInfo> page = new Pager<>(currentPage, pageSize);
        page.getFirstLimitParam();
        // 如果查询条件为空，则赋值一个空对象，避免指针异常
        if (userQuery == null) {
            userQuery = new UserQuery();
        }
        if (CommonUtil.isEmpty(userQuery.getRoleName())) {
            userQuery.setRoleName(sqlCondition);
        }
        if (CommonUtil.isEmpty(userQuery.getName())) {
            userQuery.setName(sqlCondition);
        }
        if (userQuery.getStatus() == null) {
            statusMap.put("status", sqlCondition);
        } else {
            statusMap.put("status", userQuery.getStatus());
        }
        if (userQuery.getUsertype() == null) {
            usertypeMap.put("usertype", sqlCondition);
        } else {
            usertypeMap.put("usertype", userQuery.getUsertype());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        roleNameMap.put("roleName", userQuery.getRoleName());
        nameMap.put("name", userQuery.getName());

        Object[] params = new Object[]{fromMap, toMap, roleNameMap, nameMap, statusMap, usertypeMap};
        int total = countUserRoleInfoCondition(roleNameMap, nameMap, statusMap, usertypeMap);
        List<ViewUserRoleInfo> userRoleInfoList = (List<ViewUserRoleInfo>) Execute.execute(Execute.QUERY_TYPE, baseMapper + "queryUserInfoByPage", params);
        page.setRecords(userRoleInfoList);
        page.setTotal(total);
        return page;
    }


    /**
     * 根据条件计算算法总数
     *
     * @param params: 所有的查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 8:26
     */
    private Integer countAlgorithmCondition(Object[] params) {
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE, baseMapper + "countAlgorithmCondition", params);
        if (tmp != null && tmp.size() > 0) {
            return tmp.get(0);
        }
        return -1;
    }

    /**
     * 分页查询算法信息
     *
     * @param currentPage    : 当前页数
     * @param pageSize       : 页面大小
     * @param algorithmQuery : 算法条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.VO.ViewAlgorithmInfo>
     * @author He Peng
     * @date 2021/8/8 9:51
     */
    private Pager<ViewAlgorithmInfo> queryAlgorithmInfoByPage(Integer currentPage, Integer pageSize, AlgorithmQuery algorithmQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> typeMap = new HashMap<>(1);
        Map<String, Object> titleMap = new HashMap<>(1);

        Pager<ViewAlgorithmInfo> page = new Pager<>(currentPage, pageSize);
        if (algorithmQuery == null) {
            algorithmQuery = new AlgorithmQuery();
        }
        if (CommonUtil.isEmpty(algorithmQuery.getTitle())) {
            algorithmQuery.setTitle(sqlCondition);
        }
        if (algorithmQuery.getType() == null) {
            typeMap.put("type", sqlCondition);
        } else {
            typeMap.put("type", algorithmQuery.getType());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        titleMap.put("title", algorithmQuery.getTitle());

        Object[] params = new Object[]{fromMap, toMap, titleMap, typeMap};
        Integer total = countAlgorithmCondition(new Object[]{titleMap, typeMap});
        List<ViewAlgorithmInfo> records = (List<ViewAlgorithmInfo>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryAlgorithmInfoByPage", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }


    private Integer countFeatureStatusCondition(Object[] params) {
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countFeatureStatusCondition", params);
        if (tmp != null && tmp.size() > 0) {
            return tmp.get(0);
        }
        return -1;
    }

    /**
     * 分页查询笔迹特征
     *
     * @param currentPage  : 当前页数
     * @param pageSize     : 页面大小
     * @param featureQuery : 特征条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.DO.HandwritingFeature>
     * @author He Peng
     * @date 2021/8/8 9:54
     */
    private Pager<FeatureStatus> queryFeatureByPage(Integer currentPage, Integer pageSize, FeatureQuery featureQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> featureNameMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);

        Pager<FeatureStatus> page = new Pager<>(currentPage, pageSize);

        if (featureQuery == null) {
            featureQuery = new FeatureQuery();
        }
        if (CommonUtil.isEmpty(featureQuery.getFeatureName())) {
            featureQuery.setFeatureName(sqlCondition);
        }
        if (featureQuery.getStatus() == null) {
            statusMap.put("status", sqlCondition);
        } else {
            statusMap.put("status", featureQuery.getStatus());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        featureNameMap.put("featureName", featureQuery.getFeatureName());

        Object[] params = new Object[]{fromMap, toMap, featureNameMap, statusMap};
        Integer total = countFeatureStatusCondition(new Object[]{featureNameMap, statusMap});
        List<FeatureStatus> records = (List<FeatureStatus>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryFeatureByPage", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }


    /**
     * 根据条件查询申请的总数
     *
     * @param params: 所有的查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 8:32
     */
    private Integer countAuditCondition(Object[] params) {
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countAuditCondition", params);
        if (tmp != null && tmp.size() > 0) {
            return tmp.get(0);
        }
        return 0;
    }


    /**
     * 分页查询审核记录信息
     *
     * @param currentPage : 当前页数
     * @param pageSize    : 页面大小
     * @param auditQuery  : 审核的条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.VO.ViewAuditInfo>
     * @author He Peng
     * @date 2021/8/8 9:55
     */
    private Pager<ViewAuditInfo> queryAuditInfoByPage(Integer currentPage, Integer pageSize, AuditQuery auditQuery) {

        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> nameMap = new HashMap<>(1);
        Map<String, Object> auditResultMap = new HashMap<>(1);
        Map<String, Object> beforeDateMap = new HashMap<>(1);

        Pager<ViewAuditInfo> page = new Pager<>(currentPage, pageSize);

        if (auditQuery == null) {
            auditQuery = new AuditQuery();
        }
        if (CommonUtil.isEmpty(auditQuery.getApplyName())) {
            auditQuery.setApplyName(sqlCondition);
        }
        if (auditQuery.getAuditResult() == null) {
            auditResultMap.put("auditResult", sqlCondition);
        } else {
            auditResultMap.put("auditResult", auditQuery.getAuditResult());
        }
        if (auditQuery.getBeforeDate() == null) {
            beforeDateMap.put("beforeDate", sqlCondition);
        } else {
            LocalDateTime now = LocalDateTime.now();
            now = now.minus(auditQuery.getBeforeDate(), ChronoUnit.DAYS);
            beforeDateMap.put("beforeDate", now.toString());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        nameMap.put("applyName", auditQuery.getApplyName());

        Object[] params = new Object[]{fromMap, toMap, nameMap, auditResultMap, beforeDateMap};
        Integer total = countAuditCondition(new Object[]{nameMap, auditResultMap, beforeDateMap});
        List<ViewAuditInfo> records = (List<ViewAuditInfo>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryAuditInfoByPage", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }


    /**
     * 根据条件查询备份的总数
     *
     * @param params: 查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 8:36
     */
    private Integer countBackupCondition(Object[] params) {
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countBackupCondition", params);
        if (tmp != null && tmp.size() > 0) {
            return tmp.get(0);
        }
        return 0;
    }


    /**
     * 分页查询备份记录信息
     *
     * @param currentPage : 当前页数
     * @param pageSize    : 页面大小
     * @param backupQuery : 备份的条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.DO.Backup>
     * @author He Peng
     * @date 2021/8/8 9:56
     */
    private Pager<Backup> queryBackupByPage(Integer currentPage, Integer pageSize, BackupQuery backupQuery) {

        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> beginDateMap = new HashMap<>(1);
        Map<String, Object> endDateMap = new HashMap<>(1);
        Map<String, Object> fileNameMap = new HashMap<>(1);

        Pager<Backup> page = new Pager<>(currentPage, pageSize);

        if (backupQuery == null) {
            backupQuery = new BackupQuery();
        }
        if (CommonUtil.isEmpty(backupQuery.getBeginDate())) {
            backupQuery.setBeginDate(sqlCondition);
        }
        if (CommonUtil.isEmpty(backupQuery.getEndDate())) {
            backupQuery.setEndDate(sqlCondition);
        }
        if (CommonUtil.isEmpty(backupQuery.getFileName())) {
            backupQuery.setFileName(sqlCondition);
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        fileNameMap.put("fileName", backupQuery.getFileName());
        beginDateMap.put("beginDate", backupQuery.getBeginDate());
        endDateMap.put("endDate", backupQuery.getEndDate());


        Object[] params = new Object[]{fromMap, toMap, fileNameMap, beginDateMap, endDateMap};
        Integer total = countBackupCondition(new Object[]{fileNameMap, beginDateMap, endDateMap});
        List<Backup> records = (List<Backup>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryBackupByPage", params);
        page.setTotal(total);
        page.setRecords(records);
        return page;
    }

    private Integer countCustomerHistoryInfoCondition(Object[] params) {
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countCustomerHistoryInfoCondition", params);
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }

    /**
     * <p>分页查询客户历史记录信息</p>
     * 可以查询已分析和未分析的记录
     *
     * @param currentPage : 当前页数
     * @param pageSize    : 页面大小
     * @param reportQuery : 报告的条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.VO.ViewCustomerHistoryInfo>
     * @author He Peng
     * @date 2021/8/8 9:57
     */
    private Pager<ViewCustomerHistoryInfo> queryCustomerHistoryInfo(Integer currentPage, Integer pageSize,
                                                                    ReportQuery reportQuery) {

        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> idMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);


        Pager<ViewCustomerHistoryInfo> page = new Pager<>(currentPage, pageSize);
        if (CommonUtil.isEmpty(reportQuery.getId())) {
            reportQuery.setId(sqlCondition);
        }
        if (reportQuery.getStatus() == null) {
            statusMap.put("status", sqlCondition);
        } else {
            statusMap.put("status", reportQuery.getStatus());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        idMap.put("customerId", reportQuery.getId());

        Object[] params = new Object[]{fromMap, toMap, idMap, statusMap};
        Integer total = countCustomerHistoryInfoCondition(new Object[]{idMap, statusMap});
        List<ViewCustomerHistoryInfo> records = (List<ViewCustomerHistoryInfo>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryCustomerHistoryInfo", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }

    /**
     * 根据条件查询用户历史记录总数（用于分页）
     *
     * @param params: 所有的查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 8:45
     */
    private Integer countUserHistoryInfoCondition(Object[] params) {
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countUserHistoryInfoCondition", params);
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }

    /**
     * <p>分页查询用户历史记录信息</p>
     * 可以查询已分析和未分析的记录
     *
     * @param currentPage : 当前页数
     * @param pageSize    : 页面大小
     * @param reportQuery : 报告的条件查询对象，可以为空
     * @return java.util.List<com.bluedot.www.core.pojo.VO.ViewUserHistoryInfo>
     * @author He Peng
     * @date 2021/8/8 9:58
     */
    private Pager<ViewUserHistoryInfo> queryUserHistoryInfo(Integer currentPage, Integer pageSize, ReportQuery reportQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> idMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);


        Pager<ViewUserHistoryInfo> page = new Pager<>(currentPage, pageSize);
        if (CommonUtil.isEmpty(reportQuery.getId())) {
            reportQuery.setId(sqlCondition);
        }
        if (reportQuery.getStatus() == null) {
            statusMap.put("status", sqlCondition);
        } else {
            statusMap.put("status", reportQuery.getStatus());
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        idMap.put("userId", reportQuery.getId());

        Object[] params = new Object[]{fromMap, toMap, idMap, statusMap};
        Integer total = countUserHistoryInfoCondition(new Object[]{idMap, statusMap});
        List<ViewUserHistoryInfo> records = (List<ViewUserHistoryInfo>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryUserHistoryInfo", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }

    /**
     * 根据条件获取权限总数
     *
     * @param params: 所有的查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/22 20:17
     */
    private Integer countRightCondition(Object[] params) {
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countRightCondition", params);
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }


    private Pager<Right> queryRightInfo(Integer currentPage, Integer pageSize, RightQuery rightQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> rightNameMap = new HashMap<>(1);
        Map<String, Object> urlMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);

        Pager<Right> page = new Pager<>(currentPage, pageSize);
        if (CommonUtil.isEmpty(rightQuery.getRightName())) {
            rightQuery.setRightName(sqlCondition);
        } else {
            // 模糊搜索
            rightQuery.setRightName("%" + rightQuery.getRightName() + "%");
        }
        if (CommonUtil.isEmpty(rightQuery.getUrl())) {
            rightQuery.setUrl(sqlCondition);
        } else {
            // 模糊搜索
            rightQuery.setUrl("%" + rightQuery.getUrl() + "%");
        }
        if (rightQuery.getStatus() == null) {
            statusMap.put("status", sqlCondition);
        } else {
            statusMap.put("status", rightQuery.getStatus());
        }
        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        rightNameMap.put("rightName", rightQuery.getRightName());
        urlMap.put("url", rightQuery.getUrl());

        Object[] params = new Object[]{fromMap, toMap, rightNameMap, urlMap, statusMap};
        Integer total = countRightCondition(new Object[]{urlMap, rightNameMap, statusMap});
        List<Right> records = (List<Right>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryRightInfo", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }


    private Integer countRole() {
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countRole", null);
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }

    /**
     * 分页查询，不支持条件查询
     *
     * @param currentPage: 当前页
     * @param pageSize:    页面大小
     * @return com.bluedot.www.core.pojo.DTO.Pager<com.bluedot.www.core.pojo.DO.Right>
     * @author He Peng
     * @date 2021/9/23 8:29
     */
    private Pager<Right> queryRoleInfo(Integer currentPage, Integer pageSize) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Pager<Right> page = new Pager<>(currentPage, pageSize);

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        Object[] params = new Object[]{fromMap, toMap};
        Integer total = countRole();
        List<Right> records = (List<Right>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryRoleInfo", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }

    /**
     * 条件查询所有日志数量
     *
     * @param params: 所有的日志查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/9/23 9:07
     */
    private Integer countLogCondition(Object[] params) {
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "countLogCondition", params);
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }


    /**
     * 日志条件查询
     *
     * @param currentPage 当前页
     * @param pageSize    页面大小
     * @param logQuery    日志查询条件
     * @return
     */
    private Pager<Log> queryLogInfo(Integer currentPage, Integer pageSize, LogQuery logQuery) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> beginDateMap = new HashMap<>(1);
        Map<String, Object> endDateMap = new HashMap<>(1);
        Map<String, Object> accountMap = new HashMap<>(1);
        Map<String, Object> ipAddressMap = new HashMap<>(1);
        Map<String, Object> urlMap = new HashMap<>(1);

        Pager<Log> page = new Pager<>(currentPage, pageSize);
        if (CommonUtil.isEmpty(logQuery.getAccount())) {
            logQuery.setAccount(sqlCondition);
        } else {
            // 模糊搜索
            logQuery.setAccount("%" + logQuery.getAccount() + "%");
        }
        if (CommonUtil.isEmpty(logQuery.getUrl())) {
            logQuery.setUrl(sqlCondition);
        } else {
            // 模糊搜索
            logQuery.setUrl("%" + logQuery.getUrl() + "%");
        }
        if (CommonUtil.isEmpty(logQuery.getBeginDate())) {
            logQuery.setBeginDate(sqlCondition);
        }
        if (CommonUtil.isEmpty(logQuery.getEndDate())) {
            logQuery.setEndDate(sqlCondition);
        }
        if (CommonUtil.isEmpty(logQuery.getIpAddress())) {
            ipAddressMap.put("ipAddress", sqlCondition);
        } else {
            Long ip = IpUtil.ipv4ToInt(logQuery.getIpAddress());
            ipAddressMap.put("ipAddress", ip);
        }

        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        accountMap.put("account", logQuery.getAccount());
        urlMap.put("url", logQuery.getUrl());
        beginDateMap.put("beginDate", logQuery.getBeginDate());
        endDateMap.put("endDate", logQuery.getEndDate());

        Object[] params = new Object[]{fromMap, toMap, ipAddressMap, accountMap, urlMap, beginDateMap, endDateMap};
        Integer total = countLogCondition(new Object[]{urlMap, ipAddressMap, accountMap, urlMap, beginDateMap, endDateMap});
        List<Log> records = (List<Log>) Execute.execute(Execute.QUERY_TYPE,
                baseMapper + "queryLogInfo", params);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }

    /**
     * 分页查询 客户时候存在未分析性格报告
     * @param currentPage: 当前页数
     * @param pageSize: 页面大小
     * @param query: 查询条件
     * @return java.util.List<com.bluedot.www.core.pojo.DO.Customer>
     * @author He Peng
     * @date 2021/10/30 9:32
     */
    private Pager<Customer> queryCustomerNoAnalysis(Integer currentPage, Integer pageSize, CustomerHistoryQuery query) {
        Map<String, Object> fromMap = new HashMap<>(1);
        Map<String, Object> toMap = new HashMap<>(1);
        Map<String, Object> userIdMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);
        Map<String, Object> nameMap = new HashMap<>(1);
        Map<String, Object> sortMap = new HashMap<>(1);
        Long userId = null;
        Integer status = null;
        String name = null;
        Integer sort = null;
        if (query.getUserId() == null) {
            return null;
        } else {
            userIdMap.put("userId", query.getUserId());
        }
        if (query.getStatus() == null) {
            query.setStatus(0);
        } else {
            if (query.getStatus() == -1) {
                statusMap.put("status", "1 OR 1");
            } else {
                statusMap.put("status", query.getStatus());
            }
        }
        if (CommonUtil.isEmpty(query.getName())) {
            nameMap.put("name", "1 OR 1");
        } else {
            nameMap.put("name", query.getName());
        }
        if (query.getSort() == null) {
            sortMap.put("sort", "1 OR 1");
        } else {
            sortMap.put("sort", query.getSort());
        }
        Pager<Customer> page = new Pager<>(currentPage, pageSize);
        fromMap.put("from", page.getFirstLimitParam());
        toMap.put("to", page.getPageSize());
        int total = countCustomerAnalysisCondition(query);
        Object[] params = new Object[]{fromMap, toMap, userIdMap, statusMap, nameMap, sortMap};
        List<Customer> records = (List<Customer>) Execute.execute(Execute.QUERY_TYPE, baseMapper + "queryCustomerNoAnalysis", params);
        page.setTotal(total);
        page.setRecords(records);
        return page;
    }

    /**
     * 客户是否存在未分析报告记录
     * @param query: 查询条件
     * @return java.lang.Integer
     * @author He Peng
     * @date 2021/10/30 16:55
     */
    private Integer countCustomerAnalysisCondition(CustomerHistoryQuery query) {
        Map<String, Object> userIdMap = new HashMap<>(1);
        Map<String, Object> statusMap = new HashMap<>(1);
        Map<String, Object> nameMap = new HashMap<>(1);
        Map<String, Object> sortMap = new HashMap<>(1);
        userIdMap.put("userId", query.getUserId());
        if (query.getStatus() == -1) {
            statusMap.put("status", "1 OR 1");
        } else {
            statusMap.put("status", query.getStatus());
        }
        if (CommonUtil.isEmpty(query.getName())) {
            nameMap.put("name", "1 OR 1");
        } else {
            nameMap.put("name", query.getName());
        }
        if (query.getSort() == null) {
            sortMap.put("sort", "1 OR 1");
        } else {
            sortMap.put("sort", query.getSort());
        }
        int total = -1;
        List<Integer> tmp = (List<Integer>) Execute.execute(Execute.QUERY_TYPE, baseMapper + "countCustomerAnalysisCondition",
                new Object[]{userIdMap, statusMap, nameMap, sortMap});
        if (tmp != null && tmp.size() > 0) {
            total = tmp.get(0);
        }
        return total;
    }
}
